<?php
// +----------------------------------------------------------------------
// | Fengli [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.yanyongwang.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 风黎 < 1280246895@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use app\common\model\QuestionBank;
use app\common\model\Questions;
use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use app\admin\controller\Permissions;
use think\Validate;

class Question extends Permissions
{
    /**题库主页
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {

         $model                  = new \app\common\model\QuestionBank();

         $where                  = array();
         $param                  = request()->param();
         if(request()->has('qb_name') && !empty($param['qb_name'])){
             $where['qb_name']     = ['like',"%{$param['qb_name']}%"];
         }
         $questions                = !count($where) ? $model->where($where)->order('id desc')->paginate(config('paginate.list_rows'))
             : $model->where($where)->order('id desc')->paginate(config('paginate.list_rows'),false,['query'=>$param]);
         $this->assign('questions',$questions);

         return $this->fetch();
    }


    /**
     * 新增 or 编辑
     */
    public function addSave()
    {
        $model                  = new \app\common\model\QuestionBank();
        $questionsModel         = new Questions();
        if($this->request->isPost()){
            $post               = request()->post();


            $id                 = request()->has('id') ? intval(request()->post('id')) : 0;

            //验证数据
            $validate           = new Validate([
                ['qb_name','require','题库名称不能为空'],
            ]);
            if(!$validate->check($post))
            {
                $this->error('数据格式错误:' . $validate->getError());
            }

            //防止入库出错 验证入库数据
            $saveData           = [
                'qb_name'       => isset($post['qb_name']) ? $post['qb_name'] : '',

            ];
            if($id > 0){
                //如果ID 大于0编辑
                $key            = $model->allowField(true)->isUpdate(true)->save($saveData,['id'=>$id]);
                $message        = "编辑题库";
            }else{
                //新增菜单
                $key            = $model->allowField(true)->isUpdate(false)->save($saveData);
                $message        = "新增题库";
            }

            if($key){

                $this->success($message . '成功',url('admin/question/index') );
                //删除旧题库
            }else{
                $this->error($message . '失败');
            }

        }

        //如果有id则证明是编辑
        if(request()->has('id'))
        {
            $question_bank      = $model->find(request()->param('id'));

            $this->assign('question_bank',$question_bank);

        }
        return $this->fetch();
    }

    /**
     * 题库删除
     */
    public function delete()
    {
        if($this->request->isPost()){
            $post = $this->request->post();
            $questionBankModel  =   new QuestionBank();
            $ids      = $questionBankModel->where(['id'=>$post['id']])->delete();


            return $this->success('删除成功','admin/question/index');

        }
    }

}